package com.dcivision.framework.taglib.channel;



import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.dcivision.framework.GlobalConstant;
import com.dcivision.framework.MessageResourcesFactory;
import com.dcivision.framework.SessionContainer;


public class AjaxPagerFormatter_1 extends AbstractAjaxPager {
  
  private static final Log log = LogFactory.getLog(AjaxPagerFormatter_1.class);
  
  protected String getPageInfo(String pattern) throws Exception{
    SessionContainer sessionContainer = (SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
    String pagerInfo = MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "home.channel.pager.info_"+pattern, new Object[]{new Integer(pagerTag.getBegin()),new Integer(pagerTag.getEnd()),new Integer(pagerTag.getTotal())});
    return pagerInfo;
  }

  protected String getPageIndex()throws Exception{
    try {
      SessionContainer sessionContainer = (SessionContainer)pageContext.getSession().getAttribute(GlobalConstant.SESSION_CONTAINER_KEY);
      int i = 0;
      int m_nLastPage = 0;

      //PagerTag pagerTag = (PagerTag)findAncestorWithClass(this, PagerTag.class);

      if (pagerTag!=null) {
        m_nLastPage = pagerTag.getLastPageNumber(pagerTag.getTotal());
        // if m_nLastPage > maxIndexPages, put currentPage in middle of index
        int currentPage = pagerTag.getPageNumber();
        if (m_nLastPage > pagerTag.getMaxIndexPages()) {
          i = currentPage - (pagerTag.getMaxIndexPages() / 2);
          if (i < 0) {
            i = 0;
          }

          if ((m_nLastPage - i) < pagerTag.getMaxIndexPages()) {
            i -= pagerTag.getMaxIndexPages() - (m_nLastPage - i);
          }

          if (m_nLastPage > (i + pagerTag.getMaxIndexPages())) {
            m_nLastPage = i + pagerTag.getMaxIndexPages();
          }
        }
        StringBuffer buffer = new StringBuffer();
        if(pagerTag.hasPrevPage() || pagerTag.hasNextPage()) {
            if(pagerTag.hasNextPage()){
                buffer.append("                        <a href=\"javascript:"+pagerGotoPage(pagerTag.getLastPageUrl())+"\"><div class=\"lastPage\"></div></a>");
                buffer.append("                        <a href=\"javascript:"+pagerGotoPage(pagerTag.getNextPageUrl())+"\"><div class=\"nextPage\"></div></a>");
            }else{
                buffer.append("                        <div class=\"lastPageDis\"></div>");
                buffer.append("                        <div class=\"nextPageDis\"></div>");
            }
            buffer.append("                        <select name=\"").append("selectPager\"").append(" onChange=\""+pagerGotoPage("this.value")+";\" class=\"pageSelector\">");
            while (i<m_nLastPage) {
                String url=pagerTag.getPageUrl(i);
                Integer pageNumber = pagerTag.getPageNumber(i);
                //buffer.append("&nbsp;");
                if (i==currentPage) {
                    if (pageNumber!=null) {
                        buffer.append("                            <option value=\""+url+"\" selected>"+MessageResourcesFactory.getMessage(sessionContainer.getSessionLocale(), "common.pager.index", String.valueOf(pageNumber))+"</option>");
                    }
                }else{
                    buffer.append("                            <option value=\""+url+"\">");
                    if (pageNumber!=null) {
                        buffer.append(pageNumber.intValue());
                    }
                    buffer.append("</option>");
                }
                i++;
            }
            buffer.append("                        </select>");
            if(pagerTag.hasPrevPage()){
              buffer.append("                        <a href=\"javascript:"+pagerGotoPage(pagerTag.getPrevPageUrl())+"\"><div class=\"prePage\"></div></a>");
              buffer.append("                        <a href=\"javascript:"+pagerGotoPage("1")+"\"><div class=\"firstPage\"></div></a>");
            }else{
                buffer.append("            <div class=\"prePageDis\"></div>");
                buffer.append("            <div class=\"firstPageDis\"></div>");
            }
        }

        return buffer.toString();
      }

      return null;
    } catch(Exception e) {
      log.error(e, e);
      return null;
    }
  }

}
